package com.weather.job;

import com.weather.domain.City;
import com.weather.service.CityDataService;
import com.weather.service.WeatherService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * QuartzJob定时任务
 *
 * @author: htc
 * @date: Created in 16:25 2018/5/4.
 */
public class WeatherDataSyncJob extends QuartzJobBean {
    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WeatherDataSyncJob.class);

    @Resource
    private CityDataService cityDataService;

    @Resource
    private WeatherService weatherService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("execute the job !");
        //获取城市名称列表
        List<City> cityList = null;
        try {
            cityList = cityDataService.listCity();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //遍历城市名称，获取城市天气数据
        cityList.forEach(e -> {
            try {
                weatherService.syncDataByCityName(e.getCityName());
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();
            }
        });
    }
}
